package com.qianjiangcloud.store.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import com.qianjiangcloud.store.util.LogUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    private static PayStateCallBack callBack = null;

    public interface PayStateCallBack {
        void weixinPayResult(boolean state);
    }


    public static void setPayStateCallBack(PayStateCallBack call) {
        callBack = call;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = new View(this);
        view.setLayoutParams(new LayoutParams(-1, -1));
        setContentView(view);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Toast.makeText(this, "openid = " + req.openId, Toast.LENGTH_SHORT).show();

        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                break;
            default:
                break;
        }
    }

    @Override
    public void onResp(BaseResp resp) {

        Log.d("test", "onPayFinish, errCode = " + resp.errCode + "  " + resp.errStr);
        // 回调中errCode值列表：
        // 0 成功 展示成功页面
        // -1 错误 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
        // -2 用户取消 无需处理。发生场景：用户不支付了，点击取消，返回APP。

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            int errorCode = resp.errCode;

            String msg =resp.errStr;

            LogUtils.instance.d(msg);

            if (errorCode == 0 && null != callBack) {

                callBack.weixinPayResult(true);

            } else {
                callBack.weixinPayResult(false);
            }

            finish();

        }


    }
}